/**
 * Created by mar105 on 2017-11-14.
 */
import React, { Component } from 'react';
import { Layout, Spin } from 'antd';
import MergeTableEdit from './MergeTableEdit/MoreSelectMergeTableEdit.js';
import styles from '../../index.less';
import moreSelectBaseHigher from './MoreSelectBaseHigher';
import * as config from '../../utils/config';
import * as commonFunc from './commonFunc';

class MoreSelectClassify extends Component {
  constructor(props) {
    super(props);
    this.state = {
      btnData: config.btnData, /* 工具栏按钮 */
      masterConfig: { sId: '' }, // 主表 + 从表 配置
      masterData: [],
      selectedRowKeys: [],
      sActiveId: '',
      loading: false,
      modalVisible: false, /* 弹窗显示状态 */
      modalPanel: {}, /* 弹窗面板属性 */
      listRecordKey: '', /* 编辑行 */
      resultValue: {}, /* 通用列表弹窗返回数据 */
      data: [], /* 数据 */
    };
  }

  componentWillReceiveProps(nextProps) {
    const { dataList } = nextProps.content;
    const {
      sDataListTabId, formRoute, loading, urlData, returnType,
    } = dataList;
    if ((nextProps.sTabId === sDataListTabId &&
        nextProps.formRoute === formRoute) || (nextProps.app.moreSelectPpopupPane !== undefined && nextProps.sTabId === nextProps.app.moreSelectPpopupPane.key && nextProps.formRoute === nextProps.app.moreSelectPpopupPane.route)) {
      if (loading) {
        this.setState({ loading: true });
        return;
      } else if (loading !== undefined) {
        this.setState({ loading });
      }
      if (urlData !== undefined && urlData.formData !== undefined && this.state.masterConfig.sId === '') {
        const sActiveId = this.state.masterConfig.sId;
        const masterConfig = commonFunc.getTableConfig('Classify', urlData.formData);
        this.setState({ masterConfig, sActiveId });
      }
      /* 通用分类的数据由外部保存并传入内部，用于回带可改变列表内容, 且只有面板状态modalVisible为false时才会进入 */
      if (returnType === 'isGetData' && !this.state.modalVisible) {
        const { masterConfig } = this.state;
        const { gdsconfigformslave } = masterConfig;
        const mergeConfig = commonFunc.getConfigCondition(gdsconfigformslave, 'bVisible');
        /* 整合列表数据 */
        const data = [];
        dataList.urlData.map((child) => {
          const value = {
            key: child.sId !== undefined ? child.sId : child.key,
            handleType: child.handleType,
          };
          mergeConfig.forEach((slaveChild) => {
            const ckey = slaveChild.sName;
            value[ckey] = child[ckey];
          });
          data.push(value);
          return '';
        });
        this.setState({
          data,
        });
      }
    }
  }

  shouldComponentUpdate(nextProps) {
    if (nextProps.app.ppopupPane !== undefined && nextProps.sTabId === nextProps.app.ppopupPane.key) {
      return true;
    }
    if (nextProps.sTabId === nextProps.app.currentPane.key) {
      return true;
    }
    return true;
  }

  /* 弹出框回带 */
  handleSelectBack = (selectData) => {
    this.props.handleSelectBack(selectData);
  };


  render() {
    const { loading } = this.state;
    const { pageLoading } = this.props;
    return (
      <Spin spinning={loading}>
        {pageLoading ? '' :
        <Layout className={styles.clayout}>
          <Layout className={styles.clayoutTop}>
            <MergeTableEdit
              {...this.props}
              {...this.state}
              componentName="CommonClassify"
              config={this.state.masterConfig}
              handleSelectBack={this.handleSelectBack.bind(this)}
            />
          </Layout>
        </Layout>
        }
      </Spin>
    );
  }
}

export default moreSelectBaseHigher(MoreSelectClassify);
